<?php

namespace app\task\controller;

use app\common\model\JtCronTask;
use app\common\model\JtCronTaskLog;
use app\common\model\Task;
use app\common\model\TaskLogs;
use app\common\queue\TimerStart;
use app\common\queue\TimerStop;
use Exception;
use library\Controller;
use library\service\ProcessService;
use think\Console;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\exception\HttpResponseException;
use think\exception\PDOException;
use think\facade\Log;

class Index extends Controller
{
    protected $table = 'Task';

    /**
     * @return void
     * @throws \think\Exception
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws PDOException
     * @menu true
     * @auth true
     */
    public function index()
    {
        try {
            $this->message = Console::call('stateQueue')->fetch();
            $this->command = ProcessService::instance()->think('startQueue');
            $this->listen = preg_match('/process.*?\d+.*?running/', $this->message, $attr);
        } catch (\Exception $exception) {
            $this->listen = false;
            $this->message = $exception->getMessage();
        }
        $this->title = '系统任务管理';
        $this->iswin = ProcessService::instance()->iswin();
        $db = (new Task())->order('id', 'desc');
        $this->_page($db);
    }

    public function start()
    {
        try {
            $message = nl2br(Console::call('startQueue')->fetch());
            if (preg_match('/process.*?\d+/', $message, $attr)) {
                $this->success('任务监听主进程启动成功！');
            } else {
                $this->error($message);
            }
        } catch (HttpResponseException $exception) {
            throw $exception;
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
    }

    public function stop()
    {
        try {
            $message = nl2br(Console::call('stopQueue')->fetch());
            if (stripos($message, 'succeeded')) {
                $this->success('停止任务监听主进程成功！');
            } elseif (stripos($message, 'finish')) {
                $this->success('没有找到需要停止的进程！');
            } else {
                $this->error($message);
            }
        } catch (HttpResponseException $exception) {
            throw $exception;
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
    }

    /**
     * @return void
     * @throws PDOException
     * @throws \think\Exception
     */
    public function redo()
    {
        $this->_save($this->table, ['next_run_time' => date('Y-m-d H:i:s')]);
    }

    public function run()
    {
        $this->_save($this->table, ['status' => 1]);
    }

    public function forbid()
    {
        $this->_save($this->table, ['status' => 2]);
    }

    /**
     * @return void
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @throws PDOException
     * @throws \think\Exception
     */
    public function form()
    {
        $data = [];
        if ($this->request->isPost()) {
            $data['next_run_time'] = date('Y-m-d') . ' ' . input('next_run_time');
        }
        $this->_form($this->table, '', '', '', $data);
    }

    /**
     * @return void
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @throws PDOException
     * @throws \think\Exception
     */
    public function log()
    {
        $db = (new TaskLogs())->with('task')->where('task_id',input('id'))->order('id', 'desc');
        $this->_page($db);
    }

}